#region Using Directives
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Messaging;
using Microsoft.Web.Services2.Addressing;
using System.Xml;
using Microsoft.Web.Services2.Security.Tokens;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using CSF.Communications.Sdp.ServiceLogic.Entities;
using Microsoft.ApplicationBlocks.ExceptionManagement;

#endregion

namespace CSF.Communications.Sdp.ServiceLogic.Interface
{
    #region ReceiveInterface
    /// <summary>
    /// Summary description for ReceiveInterface
    /// </summary>
    public class ReceiveInterface : SoapService
    {
        #region Members
        Process slProcess;
        #endregion

        #region Constructor
        public ReceiveInterface()
        {
           slProcess = new Process();

       }
        #endregion

        #region ReceiveControl
       [SoapMethod("http://www.microsoft.com/csf/20/ServiceLogic/Microsoft_Csf_Sbe_OrderHandling_ServiceLogic_Orchestration_GenericOrchestration_ServiceLogic/ReceiveControl")]
        public void ReceiveControl(SoapEnvelope e)
        {

            try
            {
                ReceiveControlResponse response = new ReceiveControlResponse();
                SoapEnvelope responseEnv = new SoapEnvelope();
                response.ResultCode = "0";
                responseEnv.SetBodyObject(response);
                string primaryUserId = ConfigurationManager.AppSettings["TokenUserName"], primaryUserPassword = ConfigurationManager.AppSettings["TokenPassword"];
                Uri replyUri = e.Context.Addressing.ReplyTo.Address.Value;
                Uri toUri = e.Context.Addressing.To.Value;
                From objFrom = new From(toUri);
                ReplyTo objReplyTo = new ReplyTo(toUri);
                responseEnv.Context.Addressing.From = objFrom;
                responseEnv.Context.Addressing.ReplyTo = objReplyTo;
                EndpointReference epr = new EndpointReference(replyUri);
                SlClient client = new SlClient(epr);
                client.SendWSEOneWay("http://www.microsoft.com/csf/20/ServiceLogic/Microsoft_Csf_Sbe_OrderHandling_ServiceLogic_Orchestration_GenericOrchestration_ServiceLogic/ReceiveControlResponse", responseEnv);
            }
            catch (Exception receiveControlException)
            {
                ExceptionManager.Publish(receiveControlException);
            }
                       
        }
        #endregion

        #region ReceivePayLoad
        [SoapMethod("http://www.microsoft.com/csf/20/ServiceLogic/Microsoft_Csf_Sbe_OrderHandling_ServiceLogic_Orchestration_GenericOrchestration_ServiceLogic/ReceivePayload")]
        public void ReceivePayLoad(SoapEnvelope e)
        {
            
            ServiceLogicPayloadType payload;
            CSF_MID mid_initial;
            CSF_MID mid_temp;
            string policyName;
            int majorversion=1;
            int minorversion = 3;
            VasRequestInternal vasrequest;
            SoapRequestState request;
            request = slProcess.PersistState(e);
            
            try
            {

                slProcess.UpdateServiceLogicState(request, ServiceLogicStateEnum.Started);
                payload= ServiceLogicPayloadType.DeSerializeObject(e.Body.InnerXml);
                mid_initial= slProcess.GetMid(payload);
                policyName = slProcess.GetPolicyFromDB(mid_initial);
                mid_temp = slProcess.ExecutePolicy(mid_initial, policyName, majorversion,minorversion);
                
                slProcess.ProcessNextParticipant(mid_temp, request,false, out vasrequest);

            }
             catch(Exception receivePayloadException)
            {
                ExceptionManager.Publish(receivePayloadException);
                slProcess.UpdateServiceLogicState(request, ServiceLogicStateEnum.TimedOut);
            }
        }
        #endregion

        #region CSFSessionAckResponse
        [SoapMethod("CSFSessionAckResponse")]
        public void CSFSessionAckResponse(SoapEnvelope e)
        {
            try
            {
                CSFSessionAckResponseResponse response = new CSFSessionAckResponseResponse();
                SoapEnvelope responseEnv = new SoapEnvelope();
                response.ResultCode = "0";
                responseEnv.SetBodyObject(response);
                string primaryUserId = ConfigurationManager.AppSettings["TokenUserName"], primaryUserPassword = ConfigurationManager.AppSettings["TokenPassword"];
                Uri replyUri = e.Context.Addressing.ReplyTo.Address.Value;
                Uri toUri = e.Context.Addressing.To.Value;
                From objFrom = new From(toUri);
                ReplyTo objReplyTo = new ReplyTo(toUri);
                responseEnv.Context.Addressing.From = objFrom;
                responseEnv.Context.Addressing.ReplyTo = objReplyTo;
                EndpointReference epr = new EndpointReference(replyUri);
                SlClient client = new SlClient(epr);
                client.SendWSEOneWay("CSFSessionAckResponse", responseEnv);
            }
            catch(Exception exCSFSessionAck)
            {
                ExceptionManager.Publish(exCSFSessionAck);
                
            }
        }

        #endregion

        #region CSFSessionNAckResponse
        [SoapMethod("CSFSessionNAckResponse")]
        public void CSFSessionNAckResponse(SoapEnvelope e)
        {
            try
            {
                CSFSessionNAckResponseResponse response = new CSFSessionNAckResponseResponse();
                SoapEnvelope responseEnv = new SoapEnvelope();
                response.ResultCode = "0";
                responseEnv.SetBodyObject(response);
                string primaryUserId = ConfigurationManager.AppSettings["TokenUserName"], primaryUserPassword = ConfigurationManager.AppSettings["TokenPassword"];
                Uri replyUri = e.Context.Addressing.ReplyTo.Address.Value;
                Uri toUri = e.Context.Addressing.To.Value;
                From objFrom = new From(toUri);
                ReplyTo objReplyTo = new ReplyTo(toUri);
                responseEnv.Context.Addressing.From = objFrom;
                responseEnv.Context.Addressing.ReplyTo = objReplyTo;
                EndpointReference epr = new EndpointReference(replyUri);
                SlClient client = new SlClient(epr);
                client.SendWSEOneWay("CSFSessionNAckResponse", responseEnv);
            }
            catch(Exception exCSFSessionNack)
            {
                ExceptionManager.Publish(exCSFSessionNack);

            }
        }
        #endregion

        #region ReceiveVasResponse
        [SoapMethod("http://www.microsoft.com/csf/20/ServiceLogic/Microsoft_Csf_Sbe_OrderHandling_ServiceLogic_Orchestration_GenericOrchestration_ServiceLogic/ReceiveVasResponse")]
        public void ReceiveVasResponse(SoapEnvelope e)
        {
            SoapRequestState request;
            CSF_MID mid_initial;
            CSF_MID mid_final;
            CSF_MID mid_temp;
            string policyName;
            int majorversion = 1;
            int minorversion = 3;
            VasRequestInternal vasrequest;
            request = slProcess.PersistState(e);
            
            try
            {
                
                mid_initial=slProcess.GetMidFromDB(request);
                mid_final = slProcess.GetMidAfterVASResponse(e,mid_initial);
                
                policyName = slProcess.GetPolicyFromDB(mid_final);
                mid_temp = slProcess.ExecutePolicy(mid_final, policyName, majorversion, minorversion);
                slProcess.ProcessNextParticipant(mid_temp, request, true, out vasrequest);
             }

            catch(Exception receiveVasResponseException)
            {
                ExceptionManager.Publish(receiveVasResponseException);
                slProcess.UpdateServiceLogicState(request, ServiceLogicStateEnum.TimedOut);
            }
        }
        #endregion
    }

    #endregion

    
}